package sfeir.gwt.ergosoom.client.profile;

import sfeir.gwt.ergosoom.client.ClientApi;
import sfeir.gwt.ergosoom.client.ClientApiAsync;
import sfeir.gwt.ergosoom.client.Messages;
import sfeir.gwt.ergosoom.client.faq.FaqStackPanel;
import sfeir.gwt.ergosoom.client.model.Profile;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.i18n.client.Dictionary;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class EditProfileEntryPoint implements EntryPoint, CloseHandler<PopupPanel>, ChangeHandler {
    private Messages message = GWT.create(Messages.class);

    private Profile profile;
    private EditProfile editprofile;
    private LoginDialog login;

    @Override
    public void onModuleLoad() {
        profile = null;
        // DOM.getElementById(id);
        String email = null;
        String pass = null;
        try {
            email = Dictionary.getDictionary("Credentials").get("login");
            pass = Dictionary.getDictionary("Credentials").get("pass");
        } catch (Exception e) {
        }
        if (null == email) {
            createAndShowLoginDialog();
        } else {
            ClientApiAsync client = GWT.create(ClientApi.class);
            RootPanel.get("content").getElement().setInnerText(message.authentificating());
            client.login(email, pass, false, new AsyncCallback<Profile>() {
                @Override
                public void onSuccess(Profile result) {
                    RootPanel content = RootPanel.get("content");
                    content.getElement().setInnerText("Loading ...");
                    profile = result;
                    RootPanel.get("logout").setVisible(true);
                    updateMenu();
                    editprofile = new EditProfile(result);
                    editprofile.addChangeHandler(EditProfileEntryPoint.this);
                    content.getElement().setInnerText("");
                    content.add(editprofile);
                }

                @Override
                public void onFailure(Throwable caught) {
                    createAndShowLoginDialog();
                }
            });
        }

        if (editprofile != null)
            RootPanel.get("content").add(editprofile);

        updateMenu();        
    }

    private void updateMenu() {
        Element menu_faq = RootPanel.get("faq").getElement();
        Element menu = RootPanel.get("menu_list").getElement();
        if (profile != null) {
            Element menu_editprofile = DOM.getElementById("menu_editprofile");
            Element menu_viewprofile = DOM.getElementById("menu_viewprofile");
            Element menu_login = DOM.getElementById("id_login");
            
    
            if (menu_login != null) {
                menu.removeChild(menu_login.getParentNode());
            }
            
            if (menu_editprofile == null) {
                menu_editprofile = DOM.createAnchor();
                Element liEdit = DOM.createElement("li");
                menu_editprofile.setId("menu_editprofile");
                liEdit.appendChild(menu_editprofile);
                menu.insertBefore(liEdit, menu_faq);
            }
            menu_editprofile.setAttribute("href", "#");
            menu_editprofile.setInnerText(message.edit_profile());
            DOM.setEventListener(menu_editprofile, new EventListener() {
                @Override
                public void onBrowserEvent(Event event) {
                    if (Event.ONCLICK == event.getTypeInt()) {
                        RootPanel.get("content").clear();
                        if (editprofile != null) {
                            RootPanel.get("content").add(editprofile);
                        }
                        else
                            RootPanel.get("content").add(new Label(message.error_must_logging()));
                        event.preventDefault();
                    }
                }
            });
            DOM.sinkEvents(menu_editprofile, Event.ONCLICK);
            
    
            if (menu_viewprofile == null) {
                menu_viewprofile = DOM.createAnchor();
                Element liView = DOM.createElement("li");
                menu_viewprofile.setId("menu_viewprofile");
                liView.appendChild(menu_viewprofile);
                menu.insertBefore(liView, menu_faq);
            }
            menu_viewprofile.setAttribute("href", "/profile/" + profile.getAlias());
            menu_viewprofile.setInnerText(message.view_profile());
            DOM.setEventListener(menu_viewprofile, new EventListener() {
                @Override
                public void onBrowserEvent(Event event) {
                    if (Event.ONCLICK == event.getTypeInt()) {
                        if (editprofile != null && editprofile.getIsSave()) {
                            return;
                        }
                        RootPanel.get("content").clear();
                        Profile p = getProfile();
                        if (p != null) {
                            Label LabNotSave = new Label();
                            LabNotSave.setText(message.profile_not_saved());
                            LabNotSave.setStylePrimaryName("message-red");
                            RootPanel.get("content").add(LabNotSave);
                            RootPanel.get("content").add(new ViewProfile(p));
                        }
                        else
                            RootPanel.get("content").add(new Label(message.error_must_logging()));
                        event.preventDefault();
                    }
                }
            });
            DOM.sinkEvents(menu_viewprofile, Event.ONCLICK);
            
        }
        DOM.setEventListener(menu_faq, new EventListener() {
            @Override
            public void onBrowserEvent(Event event) {
                if (Event.ONCLICK == event.getTypeInt()) {
                    if (editprofile == null) {
                        return;
                    }
                    RootPanel.get("content").clear(); //$NON-NLS-1$
                    RootPanel.get("content").getElement().setInnerHTML(""); //$NON-NLS-1$ //$NON-NLS-2$
                    RootPanel.get("content").add(new FaqStackPanel()); //$NON-NLS-1$
                    event.preventDefault();
                }
            }
        });
        DOM.sinkEvents(menu_faq, Event.ONCLICK);
    }
    
    private void createAndShowLoginDialog() {
        if (null != login)
            return;
        login = new LoginDialog();
        login.addCloseHandler(this);
        login.show();
    }

    protected Profile getProfile() {
        if (editprofile != null)
            return editprofile.getEditProfile();
        return profile;
    }

    @Override
    public void onClose(CloseEvent<PopupPanel> event) {
        RootPanel content = RootPanel.get("content");
        content.getElement().setInnerText("Loading ...");
        profile = login.getProfile();
        if (profile != null) {
            RootPanel logout = RootPanel.get("logout");
            logout.setVisible(true);
            logout.removeStyleName("hidden");
            updateMenu();
            editprofile = new EditProfile(profile);
            editprofile.addChangeHandler(EditProfileEntryPoint.this);
            content.getElement().setInnerText("");
            content.add(editprofile);
        } else {
            content.getElement().setInnerText("");
            content.add(new Label(message.error_must_logging()));
        }
    }

    @Override
    public void onChange(ChangeEvent event) {
        if (editprofile != null) {
            profile = editprofile.getProfile();
            updateMenu();
        }
    }

}
